package database;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;

import lemmatizer.Lemmatizer;

//import lemmatizer.Lemmatizer;

import parseur.Parseur;
import sparqlclient.SparqlAnswerer;


public class Main {
	
	enum TypeParsing {PARAGRAPHE, QUERIES, QUERIES_SPARQL};
	
	public static void main(String[] args) {
		
		TypeParsing typeParsing = TypeParsing.QUERIES_SPARQL;
		
		// Récupération du bon chemin de dossier
		String diallo_dir = "C:/Users/DIALLO/Desktop/ETUDE/5IR/IL/WebSem/";
		String benj_dir = "C:/Users/BenJ/Documents/Cours/5I/WebSem/";
		String web_sem_dir = "";
		
		File file = new File(diallo_dir);
		if (file.exists())
			web_sem_dir += diallo_dir;
		else {
			file = new File(benj_dir);
			if (file.exists())
				web_sem_dir += benj_dir;
		}
		
		if (web_sem_dir.length() != 0) {
			// Création des chemins de fichiers et dossiers requis
			String collectionDirectory = web_sem_dir + "Collection/";
			String queriesFile = web_sem_dir + "Queries/queries.xml";
			String queriesAnswerDirectory = web_sem_dir + "QueriesAnswer/";
			String queriesSparqlAnswerDirectory = web_sem_dir + "QueriesSparqlAnswer/";
			String queriesQrelDirectory = web_sem_dir + "qrels/";
			
			AppUtil dao = new AppUtil();
			dao.connectBase();
//			test de la fonction isFollowed
//			try {
//				System.out.println("Is Followed "+dao.isFollowed(1, "hors", "monde"));
//			} catch (Exception e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
			Parseur parseur = new Parseur(dao);
			
			System.out.println(Lemmatizer.lemme("est"));
			System.out.println(Lemmatizer.lemme("mangeais"));
			System.out.println(Lemmatizer.lemme("utilité"));
			System.out.println(Lemmatizer.lemme("sommant"));
			System.out.println(Lemmatizer.lemme("moyenne"));
			System.out.println(Lemmatizer.lemme("pommiers"));
			
			if (typeParsing == TypeParsing.PARAGRAPHE) { //Traitement des documents
				System.out.println("Traitement Paragraphes commencé à "+ new Date() );
				
				parseur.parseParagraphes(collectionDirectory);
				
				System.out.println("Traitement Paragraphes fini à "+ new Date() );
			}
			else if (typeParsing == TypeParsing.QUERIES) { //Traitement des requêtes sans Sparql
				System.out.println("Traitement Queries commencé à "+ new Date() );
				
				parseur.parseQueries(queriesFile, queriesAnswerDirectory, queriesQrelDirectory, false, null);
				
				System.out.println("Traitement Queries fini à "+ new Date() );
			}
			else {
				System.out.println("Traitement Queries avec SPARQL commencé à "+ new Date() );
				
				SparqlAnswerer sparqlAns = new SparqlAnswerer("localhost:3030/space");
				if (sparqlAns.serverIsUp()) {
					
					ArrayList<String> termes = new ArrayList<String>();
					termes.add("montagne");
					termes.add("balade");
					termes.add("animal");
					/*for (String terme : termes) {
						System.out.println("terme :" + terme);
						ArrayList<String> synonymes = sparqlAns.getSynonymes(terme);
						System.out.println("synonymes :" + synonymes);
						ArrayList<String> specialises = sparqlAns.getTermesSpecialisant(terme);
						System.out.println("specialisés :" + specialises);
						ArrayList<String> entites = sparqlAns.getEntites(terme);
						System.out.println("entités :" + entites);
					}*/
					
					parseur.parseQueries(queriesFile, queriesSparqlAnswerDirectory, queriesQrelDirectory, true, sparqlAns);
					
					/*Lemmatizer lemma = new Lemmatizer();
					try {
						System.out.println(lemma.lemme("est"));
						System.out.println(lemma.lemme("mangeais"));
						System.out.println(lemma.lemme("utilité"));
						System.out.println(lemma.lemme("sommant"));
						System.out.println(lemma.lemme("moyenne"));
						System.out.println(lemma.lemme("pommiers"));
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}*/
				}

				System.out.println("Traitement Queries avec SPARQL fini à "+ new Date() );
			}
			
			dao.close();
			Lemmatizer.destroy();
		}
	}
}
